<template>
  <div class="post">
    <!-- Page Header -->
    <header class="masthead">
      <img src = "@/assets/img/home-bg.jpg" alt="背景图片" class="background-image">
      <div class="container position-relative px-4 px-lg-5">
        <div class="row gx-4 gx-lg-5 justify-content-center">
          <div class="col-md-10 col-lg-8 col-xl-7">
            <div class="post-heading">
              <h1>{{ post.title }}</h1>
              <h2 class="subheading" v-if="post.subtitle">{{ post.subtitle }}</h2>
              <span class="meta">
                发布于 {{ post.publish_time }} · {{ post.author_name }}
              </span>
            </div>
          </div>
        </div>
      </div>
    </header>

    <!-- Post Content-->
    <article class="mb-4">
      <div class="container px-4 px-lg-5">
        <div class="row gx-4 gx-lg-5 justify-content-center">
          <div class="col-md-10 col-lg-8 col-xl-7">
            <div class="post-body" v-html="post.content"></div>
            
            <div class="post-tags" v-if="post.tags && post.tags.length">
              <h3>标签：</h3>
              <ul class="tags-list">
                <li v-for="tag in post.tags" :key="tag" class="tag">
                  {{ tag }}
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </article>


  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { articleApi } from '@/api/api'

const route = useRoute()
const post = ref({
  title: '',
  date: '',
  author: '',
  content: '',
  tags: []
})

onMounted(async () => {
  const postId = route.params.id
  try {
    const response = await articleApi.getArticleDetail(postId)
    console.log('获取到的文章详情：', response)
    const article = response[0]
    post.value = {
        title: article.title,
        publish_time: article.publish_time,
        author_name: article.author_name,
        content: article.content,
        tags: article.tags || []
      }
    } catch (error) {
    console.error('获取文章详情失败：', error)
    // 可以添加错误提示组件来展示错误信息
    alert('获取文章详情失败，请稍后重试')
  }
})
</script>

<style scoped>


/* Header */
.masthead {
  position: relative;
  margin-bottom: 3rem;
  padding-top: 8rem;
  padding-bottom: 8rem;
  background-color: #6c757d;
  border-radius: 0 0 30px 30px;
  box-shadow: 0 10px 20px -5px rgba(0, 0, 0, 0.1);
  overflow: hidden;  /* 确保圆角生效 */
  min-height: 400px;  /* 确保有足够高度显示图片 */
}

.masthead .background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  z-index: 1;  /* 图片在最底层 */
  border-radius: 0 0 30px 30px;  /* 添加圆角 */
}

.masthead:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #212529;
  opacity: 0.5;
  z-index: 2;  /* 遮罩层在图片之上 */
  border-radius: 0 0 30px 30px;  /* 确保遮罩层也有圆角 */
}

.masthead .container {
  position: relative;
  z-index: 3;  /* 确保内容在遮罩层之上 */
}

.masthead .post-heading {
  color: white;
  text-align: left;
}
/* Post Content */
.post-body {
  font-size: 1.25rem;
  color: #212529;
  line-height: 1.8;
  margin: 2rem 0;
}

.post-body p {
  margin: 2rem 0;
}

.post-body h2 {
  font-weight: 800;
  margin-top: 3rem;
  margin-bottom: 1rem;
}

.post-body img {
  max-width: 100%;
  height: auto;
  margin: 2rem 0;
}

.post-body blockquote {
  font-style: italic;
  color: #6c757d;
  margin: 2rem 0;
  padding: 1rem 2rem;
  border-left: 0.25rem solid #dee2e6;
}

/* Tags */
.post-tags {
  margin: 3rem 0;
  padding: 2rem;
  background: #f8f9fa;
  border-radius: 15px;
}

.post-tags h3 {
  color: #343a40;
  font-size: 1.2rem;
  margin-bottom: 1rem;
}

.tags-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.tag {
  background: #e9ecef;
  color: #495057;
  padding: 0.5rem 1rem;
  border-radius: 2rem;
  font-size: 0.875rem;
  transition: all 0.2s;
}

.tag:hover {
  background: #dee2e6;
  color: #212529;
}


@media (min-width: 992px) {
  #mainNav {
    border-bottom: 1px solid transparent;
    background: transparent;
  }
  #mainNav .navbar-brand {
    color: #fff;
  }
  #mainNav .navbar-brand:focus, #mainNav .navbar-brand:hover {
    color: rgba(255, 255, 255, 0.8);
  }
  #mainNav .navbar-nav > li.nav-item > a.nav-link {
    color: #fff;
  }
  #mainNav .navbar-nav > li.nav-item > a.nav-link:focus, #mainNav .navbar-nav > li.nav-item > a.nav-link:hover {
    color: rgba(255, 255, 255, 0.8);
  }
}

@media (min-width: 1200px) {
  .masthead .post-heading h1 {
    font-size: 3.5rem;
  }
  .masthead .post-heading .subheading {
    font-size: 1.875rem;
  }
}
</style>